package com.lhd._05mvcboot03.controller;

import com.lhd._05mvcboot03.common.response.JsonResult;
import com.lhd._05mvcboot03.common.response.StatusCode;
import com.lhd._05mvcboot03.mapper.UserArticleMapper;
import com.lhd._05mvcboot03.pojo.dto.UserArticleDTO;
import com.lhd._05mvcboot03.pojo.dto.UserLoginDTO;
import com.lhd._05mvcboot03.pojo.entity.Article;
import com.lhd._05mvcboot03.pojo.vo.ArticleVO;
import com.lhd._05mvcboot03.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;

/**
 * @author lhd
 * @date 2024/4/26
 * @apiNote
 */
@Slf4j
@Api(tags = "用户文章模块")
@RestController
@RequestMapping(value ="/v1/users")
@Validated
public class UserArticleController {
    @Autowired
    private UserArticleMapper userArticleMapper;

    @PostMapping(value ="login")
    @ApiOperation(value ="登录功能")
    public JsonResult login(@RequestBody @Validated UserLoginDTO userLoginDTO, @ApiIgnore HttpSession session) {
        log.debug("用户登录" + userLoginDTO);
        UserVO userVO = userArticleMapper.selectUserByUsernameAndPassword(userLoginDTO.getUsername(),userLoginDTO.getPassword());
        if (userVO == null) {
            return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
        }
        session.setAttribute("user", userVO);
        return JsonResult.ok(StatusCode.OPERATION_SUCCESS);
    }

    @PostMapping(value ="article")
    @ApiOperation("发布文章功能")
    public JsonResult article(@RequestBody @Validated UserArticleDTO userArticleDTO,@ApiIgnore HttpSession session){
        log.debug("用户发布文章" + userArticleDTO);
        UserVO userVO = (UserVO)session.getAttribute("user");
        //检验是否登录
//        if (userVO == null){
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        }
        Article article = new Article();
        article.setUserId(userVO.getId());
        BeanUtils.copyProperties(userArticleDTO, article);

        userArticleMapper.insertArticle(article);

        return new JsonResult(StatusCode.OPERATION_SUCCESS);
    }

    @ApiOperation("用户查看文章")
    @ApiImplicitParam(name = "id", value = "文章ID", required = true, dataType = "int")
    @GetMapping(value ="selectByArticleId")
    public JsonResult selectByArticleId(@Range(min = 1, max = 100,message = "文章id必须在1-100之间")
                                            @RequestParam("id")Long id, @ApiIgnore HttpSession session){
        log.debug("用户查看文章" + id);
        UserVO userVO = (UserVO)session.getAttribute("user");
//        if (userVO == null){
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        }

        ArticleVO articleVO = userArticleMapper.selectByArticleId(id);
        return new JsonResult(StatusCode.OPERATION_SUCCESS, articleVO);
    }
}
